<template>
  <div class="box">
    <el-form
      :model="loginForm"
      :rules="rules"
      ref="loginForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="loginForm.username" clearable></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="loginForm.password"
          show-password
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { userLogin } from "@/request/api";
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 7, message: "长度在 3 到 7 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    login() {
      this.$refs.loginForm.validate((flag) => {
        if (flag) {
          console.log(this.loginForm);
          userLogin(this.loginForm).then((res) => {
            console.log(res, "用户登录");
            if (res.code == 200) {
              this.$message.success(res.msg);
            //   sessionStorage.setItem('userinfo',JSON.stringify(res.list))
            this.$store.dispatch('changeUserAction',res.list)
              this.$router.push("/index");
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          alert("验证失败");
        }
      });
    },
  },
};
</script>

<style lang="less"scoped>
.box {
  width: 100wh;
  height: 100vh;
  text-align: center;
  background: linear-gradient(#ccffff, #ffcccc);

  .demo-ruleForm {
    position: absolute;
    width: 400px;
    height: 300px;
    top: 300px;
    left: 700px;
  }
}
</style>
